package com.gf.user.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gf.base.type.CareType;
import com.gf.common.bean.Page;
import com.gf.common.util.StrArrayHelper;
import com.gf.user.dao.IdolFanDao;
import com.gf.user.dao.UserBaseDao;
import com.gf.user.model.User;
import com.gf.user.service.IdolFanService;
@Transactional
@Service("idolFanService")
public class IdolFanServiceImpl implements IdolFanService {

	@Autowired
	private IdolFanDao idolFanDao;

	@Autowired
	private UserBaseDao userBaseDao;

	@Override
	public void initIdolFan(long userId) {
		idolFanDao.insertIdolFan(userId);
	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int addIdol(long userId, long idolId) {
		String idolstr = idolFanDao.queryIdols(userId);
		List<Long> list = StrArrayHelper.strToArray2(idolstr, "-");
		User u = userBaseDao.queryUserById(userId);
		userBaseDao.updateCareCount(userId, u.getCareCount() + 1);

		int result1 = addFan(idolId, userId);// userId成为了idolId的粉丝
		// 无关注
		if (list.size() == 0) {
			idolstr = "" + idolId;
			boolean result = idolFanDao.updateIdol(userId, idolstr);

			if (result && result1 != -1) {
				return 1;
			} else {
				return -1;
			}
		}

		if (!list.contains(idolId)) {
			idolstr += ("-" + idolId);
			boolean result = idolFanDao.updateIdol(userId, idolstr);
			if (result && result1 != -1) {
				return 1;
			} else {
				return -1;
			}
		} else {
			return 0;
		}

	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int addFan(long userId, long fanId) {
		String fanstr = idolFanDao.queryFans(userId);

		List<Long> list = StrArrayHelper.strToArray2(fanstr, "-");
		// 无粉丝
		User u = userBaseDao.queryUserById(userId);
		userBaseDao.updateFansCount(userId, u.getFansCount() + 1);
		if (list.size() == 0) {
			fanstr = "" + fanId;
			boolean result = idolFanDao.updateFan(userId, fanstr);
			if (result) {
				return 1;
			} else {
				return -1;
			}
		}

		if (!list.contains(fanId)) {
			fanstr += ("-" + fanId);
			boolean result = idolFanDao.updateFan(userId, fanstr);
			if (result) {
				return 1;
			} else {
				return -1;
			}
		} else {
			return 0;
		}
	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int deleteIdol(long userId, long idolId) {
		String idolstr = idolFanDao.queryIdols(userId);
		List<Long> list = StrArrayHelper.strToArray2(idolstr, "-");
		if (list.contains(idolId)) {
			list.remove(idolId);
			idolstr = StrArrayHelper.listToStr(list, "-");
			boolean result = idolFanDao.updateIdol(userId, idolstr);
			User u = userBaseDao.queryUserById(userId);
			userBaseDao.updateCareCount(userId, u.getCareCount() - 1);
			int result1 = deleteFan(idolId, userId);
			if (result && result1 != -1) {
				return 1;
			} else {
				return -1;
			}
		}
		return 0;
	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int deleteFan(long userId, long fanId) {
		String fanstr = idolFanDao.queryFans(userId);
		List<Long> list = StrArrayHelper.strToArray2(fanstr, "-");
		if (list.contains(fanId)) {
			list.remove(fanId);
			fanstr = StrArrayHelper.listToStr(list, "-");
			boolean result = idolFanDao.updateFan(userId, fanstr);
			User u = userBaseDao.queryUserById(userId);
			userBaseDao.updateFansCount(userId, u.getFansCount() - 1);
			if (result) {
				return 1;
			} else {
				return -1;
			}
		}
		return 0;
	}

	@Override
	public Page getIdolByUserId(long userId, int currentPage, int pageSize) {
		String idolstr = idolFanDao.queryIdols(userId);
		List<Long> list = StrArrayHelper.strToArray2(idolstr, "-");
		// 排序
		Collections.sort(list);
		int num = list.size();

		Page page = new Page();
		page.setCurpage(currentPage);
		page.setPageSize(pageSize);
		page.setTotal(num);
		int beginIndex = page.getStartIndex();
		// 越界了，lastIndex = list.size-1;
		int lastIndex = (beginIndex + pageSize > num) ? (num)
				: (beginIndex + pageSize);

		List<User> idols = new ArrayList<User>();
		for (int i = beginIndex; i < lastIndex; i++) {
			User user = userBaseDao.queryUserById(list.get(i));
			// 为安全起见，置空密码为空
			user.setPassword("0");
			user.setCellphone("0");
			user.setIdNumber("0");
			user.setRealName("0");
			user.setBirthday(null);
			user.setBirthdayString("0");
			user.setEmail("0");
			user.setIdPath("0");
			
			idols.add(user);
		}
		page.setResults(idols);

		return page;
	}

	@Override
	public Page getFanByUserId(long userId, int currentPage, int pageSize) {
		String fanstr = idolFanDao.queryFans(userId);
		String idolstr = idolFanDao.queryIdols(userId);
		List<Long> list = StrArrayHelper.strToArray2(fanstr, "-");
		// 排序
		Collections.sort(list);
		int num = list.size();

		Page page = new Page();
		page.setCurpage(currentPage);
		page.setPageSize(pageSize);
		page.setTotal(num);
		int beginIndex = page.getStartIndex();
		// 越界了，lastIndex = list.size-1;
		int lastIndex = (beginIndex + pageSize > num) ? (num)
				: (beginIndex + pageSize);

		List<User> fans = new ArrayList<User>();
		for (int i = beginIndex; i < lastIndex; i++) {
			Long id = list.get(i);
			User user = userBaseDao.queryUserById(id);
			//idolstr可能为空
			if (idolstr!=null && idolstr.contains(id.toString())) {
				user.setCareType(CareType.CARE);
			} else {
				user.setCareType(CareType.UNCARE);
			}
			// 为安全起见，置空密码为空
			user.setPassword("0");
			user.setCellphone("0");
			user.setIdNumber("0");
			user.setRealName("0");
			user.setBirthday(null);
			user.setBirthdayString("0");
			user.setEmail("0");
			user.setIdPath("0");
			
			fans.add(user);
		}
		page.setResults(fans);

		return page;
	}

	@Override
	public boolean isYourIdol(long userId, long idolId) {
		String idolstr = idolFanDao.queryIdols(userId);
		List<Long> list = StrArrayHelper.strToArray2(idolstr, "-");
		if (list.contains(idolId)) {
			return true;
		}
		return false;
	}

	@Override
	public boolean isYourFan(long userId, long fanId) {
		String fanstr = idolFanDao.queryFans(userId);
		List<Long> list = StrArrayHelper.strToArray2(fanstr, "-");
		if (list.contains(fanId)) {
			return true;
		}
		return false;
	}

	@Override
	public int getIdolNumber(long userId) {
		String idolStr = idolFanDao.queryFans(userId);
		List<Long> list = StrArrayHelper.strToArray2(idolStr, "-");

		return list.size();
	}

	@Override
	public int getFanNumber(long userId) {
		String fanStr = idolFanDao.queryIdols(userId);
		List<Long> list = StrArrayHelper.strToArray2(fanStr, "-");

		return list.size();
	}

}
